package com.example.lte.api;

import cn.dev33.satoken.stp.StpUtil;
import com.example.lte.config.BusinessLogic;
import com.example.lte.config.SysBusinessUnitEnum;
import com.example.lte.entity.LoginLogEntity;
import com.example.lte.entity.OperationLogEntity;
import com.example.lte.po.LogReq;
import com.example.lte.service.ISystemLocalService;
import com.example.lte.service.LoginLogServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author lzh
 * @date 2020/11/2 - 16:16
 */
@RestController
@Api(tags = "日志模块")
public class LogController {

    @Autowired
    private ISystemLocalService iSystemLocalService;
    @Autowired
    private LoginLogServiceImpl loginLogService;

    //
    //GET http://localhost:8081/lte/operationLogPage
//
//HTTP/1.1 200
//Vary: accept-encoding,origin,access-control-request-headers,access-control-request-method,accept-encoding
//Content-Type: application/json;charset=UTF-8
//Transfer-Encoding: chunked
//Date: Mon, 02 Nov 2020 08:39:06 GMT
//Keep-Alive: timeout=60
//Connection: keep-alive
//
//{
//  "code": 0,
//  "msg": "操作成功",
//  "data": {
//    "content": [
//      {
//        "id": 1,
//        "reqUrl": "/lte/init",
//        "reqIp": "0:0:0:0:0:0:0:1",
//        "reqToken": null,
//        "account": "",
//        "addTime": "2020-11-02 16:36:52"
//      },
//      {
//        "id": 2,
//        "reqUrl": "/lte/captcha",
//        "reqIp": "127.0.0.1",
//        "reqToken": null,
//        "account": "",
//        "addTime": "2020-11-02 16:37:00"
//      },
//      {
//        "id": 3,
//        "reqUrl": "/lte/login",
//        "reqIp": "127.0.0.1",
//        "reqToken": null,
//        "account": "",
//        "addTime": "2020-11-02 16:37:11"
//      },
//      {
//        "id": 4,
//        "reqUrl": "/lte/getPremByUserId",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:11"
//      },
//      {
//        "id": 5,
//        "reqUrl": "/lte/getIpAddr",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:20"
//      },
//      {
//        "id": 6,
//        "reqUrl": "/lte/tip",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:20"
//      },
//      {
//        "id": 7,
//        "reqUrl": "/lte/device",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:20"
//      },
//      {
//        "id": 8,
//        "reqUrl": "/lte/html",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:22"
//      },
//      {
//        "id": 9,
//        "reqUrl": "/lte/sendToFront",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:23"
//      },
//      {
//        "id": 10,
//        "reqUrl": "/lte/logout",
//        "reqIp": "127.0.0.1",
//        "reqToken": "2eefe0b1-86d3-445f-a36a-84c443b7c354",
//        "account": "ryy",
//        "addTime": "2020-11-02 16:37:25"
//      }
//    ],
//    "pageable": {
//      "sort": {
//        "sorted": true,
//        "unsorted": false,
//        "empty": false
//      },
//      "offset": 0,
//      "pageNumber": 0,
//      "pageSize": 10,
//      "paged": true,
//      "unpaged": false
//    },
//    "totalPages": 2,
//    "last": false,
//    "totalElements": 19,
//    "number": 0,
//    "size": 10,
//    "sort": {
//      "sorted": true,
//      "unsorted": false,
//      "empty": false
//    },
//    "numberOfElements": 10,
//    "first": true,
//    "empty": false
//  },
//  "timestamp": 1604306346240
//}
//
//Response code: 200; Time: 95ms; Content length: 1783 bytes
    //
    @GetMapping("/operationLogPage")
    @BusinessLogic(value = "查询操作日志",businessUnit = SysBusinessUnitEnum.LOG_UNIT)
    public Page<OperationLogEntity> getOperationLogPage(LogReq req) {
        StpUtil.checkLogin();
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(req.getPageNo(), req.getPageSize(), sort);
        return iSystemLocalService.getOperationLogPage(req.getAccount(), req.getStartDate(), req.getEndDate(), pageable);
    }


    @GetMapping("/loginLogPage")
    @BusinessLogic(value = "查询登录日志",businessUnit = SysBusinessUnitEnum.LOG_UNIT)
    public Page<LoginLogEntity> findLoginLogPage(LogReq req) {
        StpUtil.checkLogin();
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(req.getPageNo(), req.getPageSize(), sort);
        return loginLogService.findByCondition(req.getAccount(), req.getStartDate(), req.getEndDate(), pageable);
    }
}
